import { Toast } from "vant";
import { createRouter, createWebHashHistory } from "vue-router";

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            component: () => import('../pages/myHome/MyHome')
        },
        {
            path: '/home',
            component: () => import('../pages/myHome/MyHome')
        },
        {
            path: '/cart',
            component: () => import('../pages/myCart/MyCart'),
            meta:{
                isAuth:true
            },
        },
        {
            path: '/mine',
            component: () => import('../pages/mine/Mine'),
            meta:{
                isAuth:true
            },
        },
        {
            path: '/order',
            component: () => import('../pages/myOrder/MyOrder'),
            meta:{
                isAuth:true
            },
        },
        {
            path: '/store',
            component: () => import('../pages/store/MyStore')
        },
        {
            path: '/createorder',
            component: () => import('../pages/createOrder/CreateOrder'),
        },
        {
            path: '/address',
            component: () => import('../pages/address/Address'),
        },
        {
            path: '/addressedit',
            component: () => import('../pages/addressEdit/AddressEdit')
        },
        {
            path: '/login',
            component: () => import('../pages/login/Login')
        },
        {
            path: '/register',
            component: () => import('../pages/register/Register')
        },
        {
            path: '/userinfoedit',
            component: () => import('../pages/userInfoEdit/UserInfoEdit')
        },
    ],
});

router.beforeEach((to, from, next) => {
    if(to.meta.isAuth){
        if (localStorage.isLogin === "1") {
            next()
        }
        else{
            Toast.fail("请先注册登录")
            router.push("./register")
        }
    }
    else{
        next()
    }
})
export default router;